package com.mz.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.demo.dao.PermissionMapper;
import com.mz.demo.dao.RoleMapper;
import com.mz.demo.dao.RolePermissionMapper;
import com.mz.demo.entity.SysPermission;
import com.mz.demo.entity.SysRole;
import com.mz.demo.entity.SysRolePermission;
import com.mz.demo.entity.SysUser;
import com.mz.demo.entity.vo.RoleQuery;
import com.mz.demo.entity.vo.RoleVo;
import com.mz.demo.service.RoleService;
import com.mz.demo.utils.entity.PaginationVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    @Transactional
    public void addRole(RoleVo roleVo) {

        SysRole sysRole = roleVoToSysRole( roleVo );
        roleMapper.insert(sysRole);

        addRolePermission(roleVo);
    }

    @Override
    public IPage getRolePagination(PaginationVo paginationVo, RoleQuery roleQuery) {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<SysRole> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(SysRole::getCreateTime);
        roleMapper.selectPage(page, lqs);

        List<SysRole> roleList = page.getRecords();
        for( SysRole role : roleList ){
            List<SysPermission> rolePermissionList = getRolePermissionList(role.getId());
            role.setPermissionList(rolePermissionList);
        }

        return page;
    }

    //这个角色列表的返回不需要带权限信息，就用于 el-select 的展示
    @Override
    public List<SysRole> getRolelist(RoleQuery roleQuery) {

        LambdaQueryWrapper<SysRole> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(SysRole::getCreateTime);
        List<SysRole> list =  roleMapper.selectList(lqs);
        return list;
    }

    @Override
    @Transactional
    public void updateRoleInfo(RoleVo roleVo) {

        SysRole sysRole = roleVoToSysRole( roleVo );
        roleMapper.updateById(sysRole);

        //清除之前的对应的所有权限
        LambdaQueryWrapper<SysRolePermission> lqs = new LambdaQueryWrapper<>();
        lqs.eq(SysRolePermission::getRoleId, roleVo.getRoleId());
        rolePermissionMapper.delete(lqs);

        addRolePermission(roleVo);
        return;
    }

    @Override
    @Transactional
    public void removeRoleById(Long roleId) {

        roleMapper.deleteById(roleId);
        return;
    }


    private SysRole roleVoToSysRole( RoleVo roleVo ){

        SysRole sysRole = new SysRole();
        sysRole.setId(roleVo.getRoleId())
                .setRoleCode(roleVo.getRoleCode())
                .setRoleName(roleVo.getRoleName());
        return sysRole;
    }

    private void addRolePermission( RoleVo roleVo ){
        List<Long> permissionIdList = roleVo.getPermissionIdList();
        if( permissionIdList == null || permissionIdList.size() == 0 ){
            return;
        }

        for( Long permissionId : permissionIdList ){
            SysRolePermission sysRolePermission = new SysRolePermission()
                    .setRoleId(roleVo.getRoleId())
                    .setPermissionId(permissionId);
            rolePermissionMapper.insert(sysRolePermission);
        }
    }

    private List<SysPermission> getRolePermissionList( Long roleId ){

        LambdaQueryWrapper<SysRolePermission> lqs = new LambdaQueryWrapper<>();
        lqs.eq( SysRolePermission::getRoleId, roleId );
        List<SysRolePermission> rolePermissionList = rolePermissionMapper.selectList(lqs);
        if( rolePermissionList == null || rolePermissionList.size() == 0 ){
            return new ArrayList<>();
        }

        Set<Long> permissionIdList = rolePermissionList.stream().map(item->item.getPermissionId()).collect(Collectors.toSet());
        List<SysPermission> sysPermissionList = permissionMapper.selectBatchIds(permissionIdList);
        return sysPermissionList;
    }
}
